//
//  PublicForIpadViewController.m
//  RoadMaintain
//
//  Created by 岳学坤 on 2017/5/23.
//  Copyright © 2017年 岳学坤. All rights reserved.
//

#import "PublicForIpadViewController.h"
#import "PublicTableViewCell.h"
#define ListCellID @"cellid1"
@interface PublicForIpadViewController ()<UIWebViewDelegate>
{
    NSInteger _selectedIndex;//选中查看第几个
  
}
@property (nonatomic) NSInteger state;//0未上报  1已上报
@property (nonatomic,strong) NSDictionary *dic;//获取的需要展示的ID字典
@property (nonatomic,strong) NSString * url;
@property (nonatomic,strong) UIWebView *webView;
@end

@implementation PublicForIpadViewController

#pragma mark - Life cycle
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self resetNav];
    [self setUpWebView];
    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    [self setWebData];
    
    
}
#pragma mark - Property

#pragma mark - UI
- (void)resetNav{
    self.leftTitle = @"公告列表";
    self.pullRefreshEnable = YES;
    self.pullLoadMoreEnable = YES;
    self.myTitle.text = @"公告列表";
    self.myCodeTitle.text = @"公告详情";

    [self.tableView registerClass:[PublicTableViewCell class] forCellReuseIdentifier:ListCellID];
}
- (void)setUpWebView{
    self.webView = [[UIWebView alloc] init];
    self.webView.scrollView.showsVerticalScrollIndicator = NO;
    self.webView.scrollView.showsHorizontalScrollIndicator = NO;
    self.webView.scrollView.bounces = NO;
    self.webView.backgroundColor = Bai_Color;
    self.webView.delegate = self;
    self.webView.scalesPageToFit = YES;
    [self.view addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(64);
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(335*PadWidthRate);
        make.right.mas_equalTo(0);
    }];
}
#pragma mark - UI Response

#pragma mark - Data handle
- (void)setWebData{
    [NetWorkTool getPublicDataWithPage:self.page pageSize:10 block:^(id data) {
        [self performSelector:@selector(MJRefreshStop) withObject:nil afterDelay:1];
        [MBProgressHUD hideAllHUDsForView:self.tableView animated:NO];
        if (data) {
            if ([data[@"success"] integerValue]) {
                if (self.page == 1) {
                    _selectedIndex = 0;
                    self.tableView.contentOffset = CGPointMake(0, 0);
                    [self.dataArray removeAllObjects];
                    if ([data[@"data"]isKindOfClass:[NSArray class]]) {
                        [self.dataArray addObjectsFromArray:data[@"data"]];
                    }
                }else{
                    if ([data[@"data"]isKindOfClass:[NSArray class]]) {
                        [self.dataArray addObjectsFromArray:data[@"data"]];
                    }
                }
                if ([data[@"data"]isKindOfClass:[NSArray class]]) {
                    if ([data[@"data"] count]<10&&(self.dataArray.count!=0)) {
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                    }
                }
                
                if (self.dataArray.count == 0) {
                    self.noDataView.hidden = NO;
                    self.rightTableView.tableFooterView.hidden = YES;
                    self.myCodeTitle.text = nil;
                    self.dic = nil;
                    [self.rightTableView reloadData];
                }else{
                    self.url = self.dataArray[0][@"url"];
                    [self setWebDataDetail];
                    self.noDataView.hidden = YES;
                    }
                [self.tableView reloadData];
                self.rightTableView.contentOffset = CGPointMake(0, 0);
            }else{
                if ([data[@"message"] isEqualToString:@"请求需要进行验证,请重新传入Token"]) {
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"账号被其他设备登录，请重新登录" animated:YES];
                    [self performSelector:@selector(gotoLogin) withObject:nil afterDelay:1];
                }else{
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:data[@"message"] animated:YES];
                }
            }
            
        }else{
            [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，请检查网络后重试。" animated:YES
             ];
        }
    }];
}
- (void)gotoLogin{
    [PublicMethod noTokenToLoginWithNav:self.navigationController];
}
- (void)setWebDataDetail{
    
     [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
}
#pragma mark - Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([tableView isEqual:self.tableView]) {
        return self.dataArray.count;
        
    }else{
        return 0;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:self.tableView]) {
       
        return 75*PadHeightRate;
    }else{
        if (indexPath.row==0) {
            return 68*PadHeightRate;
        }
        else{
            return 50*PadHeightRate;
        }
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:self.tableView]) {
        PublicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ListCellID];
        cell.titleLab.text = self.dataArray[indexPath.row][@"noticeTitle"];
        cell.contentLab.text = self.dataArray[indexPath.row][@"noticeContent"];
        cell.timeLabel.text = self.dataArray[indexPath.row][@"noticeTime"];
        if (_selectedIndex == indexPath.row) {
            cell.contentView.backgroundColor = Bai_Color;
            cell.line.hidden = YES;
            cell.contentView.layer.borderWidth = 0;
            cell.blueLine.hidden = NO;
        }else{
            cell.contentView.backgroundColor = [PublicMethod colorWithHexString:@"#f4f4f4" andAlpha:1];
            cell.contentView.layer.borderColor = [Line_Color CGColor];
            cell.contentView.layer.borderWidth = 1;
            cell.line.hidden = YES;
            cell.blueLine.hidden = YES;
        }
        return cell;
    }else{
        
        return nil;
        
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:self.tableView]) {
        _selectedIndex = indexPath.row;
        self.url = self.dataArray[indexPath.row][@"url"];
        [self.tableView reloadData];
        [self setWebDataDetail];
      
    }else{
        
    }
    
}


@end
